package controller

import (
	"gitee.com/Frank098/devops-app-mgt/model"
	"gitee.com/Frank098/devops-app-mgt/service"
	"github.com/gin-gonic/gin"
)

var Ingress ingress

type ingress struct{}

// @Tags Ingress
// @Summary 获取ingress的Api信息
// @Accept json
// @Produce json
// @Param ingress_name query string true "ingress名称"
// @Param namespace query string true "ingress命名空间"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result ""
// @Router /ingress/info [GET]
func (i *ingress) GetIngressInfo(ctx *gin.Context) {
	var params model.GetIngress
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	data, err := service.Ingress.GetIngressInfo(params.IngressName, params.Namespace)
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags Ingress
// @Summary 删除ingress
// @Accept json
// @Produce json
// @Param "" body model.GetIngress true "删除ingress"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result ""
// @Router /ingress/delete [DELETE]
func (i *ingress) DeleteIngress(ctx *gin.Context) {
	var params model.GetIngress
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	if err := service.Ingress.DeleteIngress(params.IngressName, params.Namespace); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: "删除ingress成功",
	})
}

// @Tags Ingress
// @Summary 创建ingress
// @Accept json
// @Produce json
// @Param "" body model.IngressCreate true "创建ingress"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result ""
// @Router /ingress/create [POST]
func (i *ingress) CreateIngress(ctx *gin.Context) {
	var params model.IngressCreate
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	if err := service.Ingress.CreateIngress(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: "创建ingress成功",
	})
}

/*
// 更新ingress
func (i *ingress) UpdateIngress(ctx *gin.Context) {
	params := new(struct {
		Namespace string `json:"namespace"`
		Content   string `json:"content"`
	})
	//PUT请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	err := service.Ingress.UpdateIngress(params.Namespace, params.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "更新Ingress成功",
		"data": nil,
	})
}

*/
